package com.tianqu.tms.www.proxy.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.FieldChecker;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.proxy.actionform.CarProxySearchInputActionForm;
import com.tianqu.tms.www.proxy.controller.CarProxyInputConfirmController;
import com.tianqu.tms.www.proxy.viewhelper.CarProxyInputViewHelper;

/***
 * 
 * 待办受理新增确认Action
 * 
 * @author Administrator
 * 
 */
public class CarProxyInputConfirmAction extends TmsAction {

	private Logger logger = Logger.getLogger(CarProxyInputConfirmAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		CarProxySearchInputActionForm form = (CarProxySearchInputActionForm) actionForm;
		CarProxyInputViewHelper view = (CarProxyInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		if(StringUtil.isNullOrBlank(form.getVin())){
			String[] param = { "车架号" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getVendorCode()) || StringUtil.isNullOrBlank(form.getModelCode())) {
			String[] param = { "厂牌型号" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getCarType())) {
			String[] param = { "车辆类型" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getCountryStyle())) {
			String[] param = { "国别" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getCarUse())) {
			String[] param = { "使用性质" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getCardType())) {
			String[] param = { "号牌种类" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		 
		if (StringUtil.isNullOrBlank(form.getRegDate())) {
			String[] param = { "登记日期" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getMemberId())) {
			String[] param = { "商户名称" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getCarOwnerIdCardNo())) {
			String[] param = { "原车主身份证/代码证" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getCarOwner())) {
			String[] param = { "原车主姓名" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getOwnerPhone())) {
			String[] param = { "原车主电话" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		if (StringUtil.isNullOrBlank(form.getOwnerAddress())) {
			String[] param = { "原车主住址" };
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.getInputForward();
		}
		
		if (!StringUtil.isNullOrBlank(form.getCarOwner()) && form.getCarOwner().length() >5) {
			if (StringUtil.isNullOrBlank(form.getAgentOwnerIdCardNo())) {
				String[] param = { "原车主代理人身份证" };
				setErrorsToRequest(request, TmsConstants.U0005, param);
				return mapping.getInputForward();
			}
			if (StringUtil.isNullOrBlank(form.getAgentName())) {
				String[] param = { "原车主代理人姓名" };
				setErrorsToRequest(request, TmsConstants.U0005, param);
				return mapping.getInputForward();
			}
			if (StringUtil.isNullOrBlank(form.getAgentPhone())) {
				String[] param = { "原车主代理人电话" };
				setErrorsToRequest(request, TmsConstants.U0005, param);
				return mapping.getInputForward();
			}
			if (StringUtil.isNullOrBlank(form.getAgentAddress())) {
				String[] param = { "原车主代理人住址" };
				setErrorsToRequest(request, TmsConstants.U0005, param);
				return mapping.getInputForward();
			}
		}
		
		if (!FieldChecker.isAlphabetNumericalSt(form.getVin()) || form.getVin().length()<6 || form.getVin().length() >17) {
			setErrorsToRequest(request, TmsConstants.U1051, null);
			return mapping.getInputForward();
		}
		
		if (!FieldChecker.isAlphabetNumericalSt(form.getCarOwnerIdCardNo()) && !"-".equals(form.getCarOwnerIdCardNo())) {
			setErrorsToRequest(request, TmsConstants.U1052, null);
			return mapping.getInputForward();
		}
		
		if(!DateUtil.isValidDate(form.getRegDate())){
			String[] param = {"有效结束日期", "YYYYMMDD"};
			setErrorsToRequest(request, TmsConstants.U0018, param); 
			return mapping.getInputForward();
		}else{
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			// 当前系统日期
			String nowDateStr = formatDate.format(new Date());
			Date nowDate = formatDate.parse(nowDateStr);
			// 有效结束日期	
			Date regDate = formatDate.parse(form.getRegDate());
			if (regDate.compareTo(nowDate) >= 0) {
				String[] param = {"有效结束日期"};
				setErrorsToRequest(request, TmsConstants.U0021, param); 
				return mapping.getInputForward();
			}	
		}
		
		CarProxyInputConfirmController controller = new CarProxyInputConfirmController();
		controller.execute(uc);
		
		view.setPrintBsflag("1");
		request.setAttribute(TmsConstants.VIEWHELP, setViewFromForm(form,view));
		return mapping.findForward("goResultAction");
	}
	
	
	
	private CarProxyInputViewHelper setViewFromForm(CarProxySearchInputActionForm form, CarProxyInputViewHelper view) throws TmsDaoException{
		if (form == null) return view;
		
		Codes code = Codes.getInstance();
		view.setSerial(form.getSerial());// 流水号
		view.setLicensePlate(form.getLicensePlate());// 车牌号码
		view.setVin(form.getVin());// vin码
		view.setAgentName(form.getAgentName());
		view.setAgentOwnerIdCardNo(form.getAgentOwnerIdCardNo());
		view.setAgentAddress(form.getAgentAddress());
		view.setOwnersGroupPhoto(form.getOwnersGroupPhoto());
		view.setMaterialImages1(form.getMaterialImages1());
		view.setMaterialImages2(form.getMaterialImages2());
		view.setMaterialImages3(form.getMaterialImages3());
		view.setMaterialImages4(form.getMaterialImages4());
		view.setMaterialImages5(form.getMaterialImages5());
		view.setMaterialImages6(form.getMaterialImages6());
		view.setMaterialImages7(form.getMaterialImages7());
		view.setMaterialImages8(form.getMaterialImages8());
		view.setMaterialImages9(form.getMaterialImages9());
		view.setMaterialImages10(form.getMaterialImages10());
		if(form.getRegDate()!=null) view.setRegDate(form.getRegDate());
		view.setAgentPhone(form.getAgentPhone());
		view.setCarType(form.getCarType());
		if(StringUtil.isNotBlank(form.getCarType())) view.setCarTypeName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE,Integer.parseInt(form.getCarType())));
		view.setCardType(form.getCardType());
		if(StringUtil.isNotBlank(form.getCardType())) view.setCardTypeName(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,Integer.parseInt(form.getCardType())));
		view.setModelCode(form.getModelCode());
		view.setVendorCode(form.getVendorCode());
		view.setOwnerAddress(form.getOwnerAddress());
		view.setCarOwner(form.getCarOwner());
		view.setOwnerPhone(form.getOwnerPhone());
		view.setRegisterCardNo(form.getRegisterCardNo());
		view.setCountryStyle(form.getCountryStyle());
		if(StringUtil.isNotBlank(form.getCountryStyle())) view.setCountryStyleName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY,Integer.parseInt(form.getCountryStyle())));
		view.setCarUse(form.getCarUse());
		if(StringUtil.isNotBlank(form.getCarUse())) view.setCarUseName(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,Integer.parseInt(form.getCarUse())));
		TransferProcessHistory his = new TransferProcessHistory();
		view.setLcList(his.loadtHistoryBySerial(form.getSerial()));
		view.setMemberId(form.getMemberId());
		view.setVendorCode(form.getVendorCode());
		view.setModelCode(form.getModelCode());
		view.setOwnerType(form.getOwnerType()+"");
		view.setCarOwnerIdCardNo(form.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(form.getOwnerOrgNo());
		view.setCarOwner(form.getCarOwner());
		view.setOwnerPhone(form.getOwnerPhone());
		view.setOwnerAddress(form.getOwnerAddress());
		view.setAgentOwnerIdCardNo(form.getAgentOwnerIdCardNo());
		view.setAgentName(form.getAgentName());
		view.setAgentPhone(form.getAgentPhone());
		view.setAgentAddress(form.getAgentAddress());
		view.setZipCode(form.getZipCode());
		return view;
	}
}
